package com.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016101700709791";

    // 商户私钥，您的PKCS8格式RSA2私钥

    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC6tGdBaCcWJn7yO/MaDnY82+hf5ek3Hli+l6kSzmFPyC1fXRkaXod1gAoQaUOdyG9OaBf67fc+ZB8wVhZdcwDuUs4vFUz0YjcWvKGvwgmk5Ws/SFY0Y17i+p6eRJ9DN68+UkVt+FttI4bC6mgVl/Cc/9Z6Azo2pcw0Yo74yxjQy+FXT2oxTSkCSmAeeI0F8IpTcDQIYA28mxltjHDfuXpu5dbBkhl/ZjVf6O28u9X/YX1okE1hdnMuagjgZQF/t+nOhoAwLZv5QG2ZItAhiy3LNFAngxpGWgsN5PROE7Fbj2dgsu2t60+1sFsIki0SFg/IrRhBob742loMU8JxpKrLAgMBAAECggEAe9+Z6oGJ/V5JN7qizpTtye81u/q0w0c1Ji9FlB6t3ds4j18beq1hPopXIxBUNml3wBO8ZVZhhlmzf8R+Tj4xTe5Oslp1lmdbmZBJzmz1LRyyDWiXGWcc24fgma155YUalkTLn+z56+14SfZwXHNUxzXPPWDKzNswI0mJBnTp4A1jBF808rAc3M32Q6j4IaFNYnWv3chTyt/Wt41xLJ6hgWydMcMl0Szc7L3eFP/B4gwDqJd0zY1EMXnx8Ql1ypb1sL/mFIrcc/fWTgNHxsXhA9bVSKM++Uiu7ik7qbStev120xIAvRq8j5MJF9RtUBhHs5PfXPQfEM2VwVIFGW7UIQKBgQDi7FG9JAP6m+9JEhHUdMnCcdB1Rk9JvuAFs/U1aCyUoncQys+VATDwe27JhK+9r0kHbcDyv7GjlpNCdf3iP+Eon/qkgyb+rT/aYDK2H4pE+TO5VJONk+DBpFyHRORtVydGrM3y+w6O2ggowl0JNyqXmEpVqkpMFuSiWAUotU4XkwKBgQDSoNAlZ5gBcwVWmauYFoq4jTz2fgFCq9B5f/CBhx45S2/gYn+7d/CVw/Mu+eZZ14ItrYAotji2r0U3UfkmjLCXGYfHcaCUDHNwQGfavZHBPAZyTRMWu3LGIMAxe/cXjoubXA9YyTzl6X64kBkz+9tZyW2EVPsV7Osj70cJ3D+y6QKBgFWu+eY7NS1pt96VbhoUxyIeq6G8nK+x3REKNGqoHT06Y0755YvT2VDUqq545YrwJtkhPhvq+f5VGuGbf/PwPuqCDbgUtaKwdE3WyQ8vtZv5pOgz4UsyLUmw8Cc7gi0vHWt4UIOQ/JxAA5iwW0ThibmYDWlvVYKJmmcaaUhHvWb1AoGBAI7AQPHo6QOqcebElaCpPnFf//5LXg1SpyOBi34uFlTchMF94HYtDthiCrI+2ljvj6HnoiF1hrCBz5ZzrznqRlDSE80ffKZ6Xc6pR/nfWyasyO20wQg6GMqhLcEb9FZ53LJ5zUjucqCcBK+PshUiUALlWp1Cfz3X6XHcXLgZFu7pAoGAHIkvPtXWJGidCpm+Y/Tu0ILfKcbg2eNot+utVXIZcFeWxZMnZ4C87qNRIfLOQ/Z3VLzEIJY8CzV6CFM00lIW38UX4egtu00BR3Tjk6YPZt0L6E46T70XK+H+KkSj4eIRH13IfOZ9HwErazE15t2Hnvs61E9yjGzvQgyBUzT8SDU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiJRnT0VVgye5+LiuF7TU7zttzwbBWGr3P4Syc6a9oCG1gxkaO0hzPmGJf02CLpjTJV31Rd/jQ2J3IXp91NnF7Lbb9PftMBva5/T/6MBpIRvs+zWCyR5j8jRU7e6wjf6Pz7+/fIU3r9duqSvn9MjaraL/Tkodf1Gp1YXkPXnBsYyfZqEapHDV20qsuGXLiXELIhotrySczrSkMVr7hTSDFl5j2LYsYl7JD4BbWcSvOn4eWgjDwweC/IqH/AXhxPXwho6kjULwBXArsVjkajKOaEUHNPq/bvynCOYslkMN09k7fL9byBcRgDGipct/YbWyXfYXJ4PAvdbPhJYGy7Nh/QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://shushu.free.idcfengye.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    private String sign_type = "RSA2";

    private String charset = "utf-8";

    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "30m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
